//
//  DKLoginAccountView.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/19.
//

#import "DKLoginAccountView.h"
#import "DKLoginVoiceAlertView.h"
@interface DKLoginAccountView()<UITextFieldDelegate>
@property (nonatomic, strong)UIButton * dk_LoginAccountBackBtn;
@property (nonatomic, strong)UILabel *dk_LoginAccountHeadLab;
@property (nonatomic, strong)UIButton * dk_LoginAccountSendBtn;

@property (nonatomic, strong) NSTimer * msgCodeTimer;
@property (nonatomic,assign) NSInteger msgCodeTime;
@end
@implementation DKLoginAccountView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = DK_Hex(@"#F7F7F8");
        self.layer.borderWidth = 0.5;
        self.layer.borderColor = DK_Hex(@"#4566FF").CGColor;
        self.layer.cornerRadius = 14;
        self.clipsToBounds = YES;
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    self.dk_LoginAccountBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.dk_LoginAccountBackBtn setImage:DK_GetImage(@"dk_registerAccountHead") forState:UIControlStateNormal];
    [self.dk_LoginAccountBackBtn addTarget:self action:@selector(goAccountReset) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.dk_LoginAccountBackBtn];
    [self.dk_LoginAccountBackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(20)));
        make.top.equalTo(@(DK_IsWidthScale(17)));
        make.bottom.equalTo(@(-DK_IsWidthScale(17)));
        make.width.equalTo(self.dk_LoginAccountBackBtn.mas_height);
    }];
    self.dk_LoginAccountHeadLab = [[UILabel alloc] init];
    self.dk_LoginAccountHeadLab.textColor = DK_Hex(@"#333333");
    self.dk_LoginAccountHeadLab.font = DK_FONT_Regular(15);
    self.dk_LoginAccountHeadLab.text = @"+91";
    [self addSubview:self.dk_LoginAccountHeadLab];
    [self.dk_LoginAccountHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_LoginAccountBackBtn.mas_right).offset(DK_IsWidthScale(9));
        make.width.equalTo(@(DK_IsWidthScale(32)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(-DK_IsWidthScale(0)));
    }];
    
    self.dk_LoginAccountTextField = [[UITextField alloc] init];
    self.dk_LoginAccountTextField.textColor = DK_Hex(@"#333333");
    self.dk_LoginAccountTextField.font = DK_FONT_Regular(15);
    self.dk_LoginAccountTextField.placeholder = @"Please enter your phone number";
    self.dk_LoginAccountTextField.delegate = self;
    self.dk_LoginAccountTextField.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
    [self addSubview:self.dk_LoginAccountTextField];
    [self.dk_LoginAccountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dk_LoginAccountHeadLab.mas_right).offset(DK_IsWidthScale(0));
        make.right.equalTo(@(-DK_IsWidthScale(70)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(-DK_IsWidthScale(0)));
    }];
    
    self.dk_LoginAccountSendBtn = [[UIButton alloc] init];
    [self.dk_LoginAccountSendBtn setTitle:@"Send" forState:UIControlStateNormal];
    [self.dk_LoginAccountSendBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.dk_LoginAccountSendBtn.titleLabel.font = DK_FONT_Medium(14);
    [self.dk_LoginAccountSendBtn setBackgroundImage:DK_GetImage(@"dk_registerSendBg") forState:UIControlStateNormal];
    [self.dk_LoginAccountSendBtn addTarget:self action:@selector(sendLoginMsgCode) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.dk_LoginAccountSendBtn];
    [self.dk_LoginAccountSendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(-DK_IsWidthScale(0)));
        make.width.equalTo(@(DK_IsWidthScale(60)));
    }];
}
-(void)setSmsType:(NSString *)smsType{
    _smsType = smsType;
    if([_smsType isEqualToString:@"reset_pwd"]){
        [self.dk_LoginAccountBackBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(DK_IsWidthScale(0)));
        }];
        [self.dk_LoginAccountHeadLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.dk_LoginAccountBackBtn.mas_right).offset(DK_IsWidthScale(0));
        }];
    }else if([_smsType isEqualToString:@"modify_bank"]){
        [self.dk_LoginAccountBackBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(DK_IsWidthScale(0)));
        }];
        [self.dk_LoginAccountHeadLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.dk_LoginAccountBackBtn.mas_right).offset(DK_IsWidthScale(0));
        }];
    }
}
-(void)setIsHidSend:(BOOL)isHidSend{
    _isHidSend = isHidSend;
    if(_isHidSend){
        [self.dk_LoginAccountTextField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.dk_LoginAccountHeadLab.mas_right).offset(DK_IsWidthScale(0));
            make.right.equalTo(@(-DK_IsWidthScale(0)));
        }];
        self.dk_LoginAccountSendBtn.hidden = YES;
    }
}
- (void)goAccountReset{
    if(self.goAccountResetBlock){
        self.goAccountResetBlock();
    }
}
- (void)sendLoginMsgCode{
    if([self.smsType isEqualToString:@"register"]){
        NSMutableDictionary * parmDic = [[NSMutableDictionary alloc] init];
        [parmDic setObject:self.dk_LoginAccountTextField.text forKey:@"phone"];
        [self requestUserBrhavRecord:DK_ActionSMSSend_Click withParmDic:parmDic];
    }
//    if(self.goAccountLoginBlock){
        if(self.dk_LoginAccountTextField.text.length <= 0){
            [self makeToast:@"Please enter your phone number"];
            return;
        }else{
            [self startTime];
//            self.goAccountLoginBlock(self.dk_LoginAccountTextField.text);
        }
//    }
}
- (void)startTime{
    WeakSelf(self)
    if(!self.msgCodeTimer){
        if([_smsType isEqualToString:@"modify_bank"]){
            [weakSelf sendMsgCode];
        }else{
            //判断验证码次数
            NSMutableDictionary *registerPhoneCountDic = [[NSMutableDictionary alloc] init];
            [registerPhoneCountDic setValue:self.dk_LoginAccountTextField.text forKey:@"phone"];
            [registerPhoneCountDic setValue:self.smsType forKey:@"type"];
            [registerPhoneCountDic setValue:@"0" forKey:@"voiceCode"];
            [DKPublicHttp apiUserSendSmsCountForAPI:registerPhoneCountDic complete:^(NSString * _Nonnull codeCount) {
                if([codeCount intValue] >= 3){
                    //语音弹框
                    [DKLoginVoiceAlertView dk_ShowVoiceAlertViewNextCompleteIsDismiss:YES withVoice:^{
                        [weakSelf sendVoiceCode];
                    } WithSms:^{
                        [weakSelf sendMsgCode];
                    }];
                }else{
                    [weakSelf sendMsgCode];
                }
            }];
        }
    }
}
- (void)startSendMsgCode{
    if(self.msgCodeTime > 0){
        self.msgCodeTime --;
        [self.dk_LoginAccountSendBtn setTitle:[NSString stringWithFormat:@"%zds",(long)self.msgCodeTime] forState:UIControlStateNormal];
    }else{
        [self endMsgTime];
    }
}
- (void)endMsgTime{
    [self.msgCodeTimer invalidate];
    self.msgCodeTimer = nil;
    [self.dk_LoginAccountSendBtn setTitle:@"Send" forState:UIControlStateNormal];
    self.msgCodeTime = 60;
}
- (void)sendMsgCode{
    if(self.dk_LoginAccountTextField.text.length <= 0){
        [self makeToast:@"Please enter your phone number"];
        return;
    }
    WeakSelf(self)
    NSMutableDictionary *registerPhoneCodeDic = [[NSMutableDictionary alloc] init];
    [registerPhoneCodeDic setValue:self.dk_LoginAccountTextField.text forKey:@"phone"];
    [registerPhoneCodeDic setValue:self.smsType forKey:@"type"];
    [registerPhoneCodeDic setValue:@"0" forKey:@"voiceCode"];
    [DKPublicHttp apiUserSendSmsForAPI:registerPhoneCodeDic complete:^{
        [DKAlertHelper showMessage:@"SMS verification code sent successfully"];
        weakSelf.msgCodeTime = 60;
        [weakSelf.msgCodeTimer invalidate];
        weakSelf.msgCodeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(startSendMsgCode) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:weakSelf.msgCodeTimer forMode:NSRunLoopCommonModes];
        [weakSelf.msgCodeTimer fire];
        
        if([weakSelf.smsType isEqualToString:@"register"]){
            NSMutableDictionary * parmDic = [[NSMutableDictionary alloc] init];
            [parmDic setObject:weakSelf.dk_LoginAccountTextField.text forKey:@"phone"];
            [weakSelf requestUserBrhavRecord:DK_ActionSMSSend_Success withParmDic:parmDic];
        }
    }];
}
- (void)sendVoiceCode{
    if([self.smsType isEqualToString:@"register"]){
        NSMutableDictionary * parmDic = [[NSMutableDictionary alloc] init];
        [parmDic setObject:self.dk_LoginAccountTextField.text forKey:@"phone"];
        [self requestUserBrhavRecord:DK_ActionVoiceSend_Click withParmDic:parmDic];
    }
    WeakSelf(self)
    NSMutableDictionary *registerPhoneCodeDic = [[NSMutableDictionary alloc] init];
    [registerPhoneCodeDic setValue:self.dk_LoginAccountTextField.text forKey:@"phone"];
    [registerPhoneCodeDic setValue:self.smsType forKey:@"type"];
    [registerPhoneCodeDic setValue:@"1" forKey:@"voiceCode"];
    [DKPublicHttp apiUserSendSmsForAPI:registerPhoneCodeDic complete:^{
        [DKAlertHelper showMessage:@"Currently sending a phone verification code, please accept the call"];
//        [weakSelf endMsgTime];
        if([weakSelf.smsType isEqualToString:@"register"]){
            NSMutableDictionary * parmDic = [[NSMutableDictionary alloc] init];
            [parmDic setObject:weakSelf.dk_LoginAccountTextField.text forKey:@"phone"];
            [weakSelf requestUserBrhavRecord:DK_ActionVoiceSend_Success withParmDic:parmDic];
        }
    }];
}
- (BOOL)isCanSendCode{
    return !self.msgCodeTimer;
}
- (void)requestUserBrhavRecord:(NSString *)action withParmDic:(NSDictionary *)parmDic{
    [DKPublicHttp apiUserBrhavRecordForAPI:parmDic withPageName:DK_Register withAction:action complete:^{
            
    } fail:^{
            
    }];
}
#pragma mark -- UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *phoneStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if(phoneStr.length > 0){
        if(self.goCheckAcountBlock){
            self.goCheckAcountBlock(false);
        }
    }else{
        if(self.goCheckAcountBlock){
            self.goCheckAcountBlock(true);
        }
    }
    return YES;
}
@end
